package com.itcast.donkey.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.adapters.TravelHeadAdapter;
import com.itcast.donkey.fragments.InHeadLikeFragment;
import com.itcast.donkey.fragments.InHeadTravelFragment;
import com.itcast.donkey.modles.TravelInHeadImageModels;
import com.itcast.donkey.modles.UriModles;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/12.
 */
public class TravelInHeadImageActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> tabName = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private TravelHeadAdapter adapter;


    private LinearLayout linTop;
    private RelativeLayout relBottom;
    private LinearLayout linButton;

    private ImageView logo;
    private Button attention, private_letter;

    private TextView userNameTop;
    private ImageView headImage;
    private TextView userName;
    private ImageView userMaleSex;
    private ImageView userFemaleSex;
    private TextView userLevel;


    //作者信息设置
    //private TravelInHeadAdapter inHeadAdapter;
    private TravelInHeadImageModels headImageModels;

    private String ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_in_head_image);

        ID = getIntent().getStringExtra("headID");
        initView();
        initData();
        getData();
        initEvent();

    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        logo = (ImageView) findViewById(R.id.logo);
        userNameTop = (TextView) findViewById(R.id.user_name_top);
        headImage = (ImageView) findViewById(R.id.head_image);
        userName = (TextView) findViewById(R.id.user_name);
        userMaleSex = (ImageView) findViewById(R.id.user_male_sex);
        userFemaleSex = (ImageView) findViewById(R.id.user_female_sex);
        userLevel = (TextView) findViewById(R.id.user_level);
        attention = (Button) findViewById(R.id.attention);
        private_letter = (Button) findViewById(R.id.private_letter);
        attention = (Button) findViewById(R.id.attention);
        private_letter = (Button) findViewById(R.id.private_letter);

    }

    private void initData() {
        tabName.add("游记");
        tabName.add("喜欢");
        InHeadTravelFragment travelFragment = new InHeadTravelFragment();
        InHeadLikeFragment likeFragment = new InHeadLikeFragment();

        fragments.add(travelFragment);
        fragments.add(likeFragment);

        //实例化适配器
        adapter = new TravelHeadAdapter(getSupportFragmentManager(), tabName, fragments);
        //设置适配器
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initEvent() {
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TravelInHeadImageActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        private_letter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TravelInHeadImageActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(UriModles.TRAVEL_FRONT_IN_Item_IMAGE_URL + ID + UriModles.TRAVEL_END_IN_Item_IMAGE_URL)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        headImageModels = gson.fromJson(result, TravelInHeadImageModels.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
//                    Toast.makeText(TravelInHeadImageActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Picasso.with(TravelInHeadImageActivity.this).load(headImageModels.getImage()).into(headImage);
                    userNameTop.setText(headImageModels.getName());
                    userName.setText(headImageModels.getName());
                    if (headImageModels.getGender() == 1) {
                        userMaleSex.setVisibility(View.VISIBLE);
                        userFemaleSex.setVisibility(View.GONE);
                    } else {
                        userMaleSex.setVisibility(View.GONE);
                        userFemaleSex.setVisibility(View.VISIBLE);
                    }
                    userLevel.setText(headImageModels.getTrips_count() + "篇游记");
                    break;
            }
        }
    };
}
